<?php
// +----------------------------------------------------------------------
// | ThinkSNS
// +----------------------------------------------------------------------
// | Copyright (c) 2008 http://www.thinksns.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Nonant <nonant@163.com>
// +----------------------------------------------------------------------

class BugAction extends BaseAction {

	function _initialize(){
		parent::_initialize();
		$this->assign('smileList',$this->getSmile($this->opts['ico_type']));
		$this->assign('smilePath',$this->getSmilePath($this->opts['ico_type']));			
	}
	
	//添加bug
	function add(){
		$this->assign('edition',D('Edition')->order('id DESC')->findall());	
		$this->assign('category',D('Category')->findall());	
		$this->display();
	}
	
	//添加Bug 操作
	function doadd(){
		$pBug = D('Bug');
		if(strlen(h($_POST['title']))<6){
			$this->error('标题不能小于6个字符');
		}
		if(empty($_POST['content'])){
			$this->error('内容不能为空');
		}
		$map['title'] = h($_POST['title']);
		if($pBug->where($map)->count()){
			$this->error('标题已存在');
		}else{
			if($pBug->create()){
				$pBug->title = h($_POST['title']);
				$pBug->uid   = $this->mid;
				$pBug->uname = $this->my_name;
				$pBug->browser = h($_POST['browser']);
				$pBug->setting = h($_POST['setting']);
				$pBug->edition = intval($_POST['edition']);
				$pBug->catid = intval($_POST['catid']);
				$pBug->content = $_POST['content'];
				$pBug->cTime   = time();
				$id=$pBug->add();
				if($id){
					$this->redirect('Bug/view/id/'.$id);
				}else{
					$this->error('提交失败');
				}
			}else{
				$this->error('提交失败');
			}
		}
	}
	
	//修改一个bug
	function edit(){
		$pBug = D('Bug');
		$intId = intval($_GET['id']);
		$info = $pBug->where('id='.$intId)->find();
		if($info && $intId){
			if($info['uid']==$this->mid || $this->_isbugadmin){
				$this->assign('buginfo',$info);			
				$this->assign('edition',D('Edition')->order('id DESC')->findall());	
				$this->assign('category',D('Category')->findall());
				$this->display();
			}else{
				$this->error('您无权修改');
			}
		}else{
			$this->error('您提交了错误的参数');
		}
	}
	
	//修改操作
	function doedit(){
		$pBug = D('Bug');
		if($pBug->create()){
			$pBug->id    = intval($_POST['id']);
			$pBug->title = h($_POST['title']);
			$pBug->edition = intval($_POST['edition']);
			$pBug->catid = intval($_POST['catid']);
			$pBug->browser = h($_POST['browser']);
			$pBug->setting = h($_POST['setting']);
			$pBug->content = h($_POST['content']);
			$pBug->save();
			$this->redirect('Bug/view/id/'.intval($_POST['id']));
		}
	}
	
	//删除
	function dodel(){
		$pBug = D('Bug');
		$intId = intval($_GET['id']);
		$info = $pBug->where('id='.$intId)->find();
		if($info && $intId){
			if($info['uid']==$this->mid || $this->_isbugadmin){
				$pBug->where('id='.$intId)->delete();
				$this->redirect('/Index/my');
			}else{
				$this->error('您无权修改');
			}
		}else{
			$this->error('您提交了错误的参数');
		}		
	}
	
	//查看
	function view(){
		$intId = intval($_GET['id']);
		$pBug = D('Bug');
		$info = $pBug->where('id='.$intId)->find();
		if($info && $intId){
			$this->assign('buginfo',$info);
			if($info['attach']){
				$attachlist = unserialize($info['attach']);
				foreach ($attachlist as $v){
					$arrs[] = $v['id'];
				}
				$attmap['id'] = array('in',$arrs);
				$this->assign('attach',D('Attach')->where($attmap)->findall());
			}
			$buglist = D('BugLog')->where('bugid='.$intId)->order('id DESC')->findall();
			$this->assign('bugloglist',$buglist);
			$this->display();
		}else{
			$this->error('提交错误参数');
		}
	}
	
	//处理bug操作
	function dotype(){
		$strType = h($_POST['type']);
		$intId = intval($_POST['id']);
		$pBug = D('Bug');
		$info = $pBug->where('id='.$intId)->find();
		if($this->_isbugadmin && $info && $intId){
			switch ($strType){
				case 'coopt':  //指派
				$pBugCoopt = D('BugCoopt');
					foreach ($_POST['userId'] as $val){
						//记录指派信息
						if($this->mid!=$val){
							$cooptdata['douid'] = $this->mid;
							$cooptdata['uid'] = $val;
							$cooptdata['bugid'] = $intId;
							$cooptdata['cTime'] = time();
							$pBugCoopt->add($cooptdata);
							unset($cooptdata);
							$html[]= '<a href="'.__TS__.'/space/'.$val.'" target="_blank">'.getUserName($val).'</a>' ;
							
            				//通知
            				$body_data['id'] = $intId;
            				$body_data['title'] = $info['title'];
            				$this->api->notify_setAppId($this->appId);
            				$this->api->notify_send($val,'bug_coopt',$title_data,$body_data,$url,'notification');
						}
					}
					//记录日志
					$data['uid']     = $this->mid;
					$data['bugid']   = $intId;
					$data['content'] = '指定 '.implode(',',$html).' 进行处理';
					$data['cTime']   = time();
					D('BugLog')->add($data);
					$pBug->setField('state',1,'id='.$intId);
					$this->redirect('Bug/view/id/'.$intId);
				break;
				
				case 'sopite':   //解决
					$content = h($_POST['content']);
					if($pBug->setField('reply',$content,'id='.$intId)){
						
						$attach =  serialize($this->__wipeVerticalArray($_POST['attach']));
						$pBug->setField('attach',$attach,'id='.$intId);			
									
						//记录日志
						$data['uid']     = $this->mid;
						$data['bugid']   = $intId;
						$data['content'] = '进行处理';
						$data['cTime']   = time();
						D('BugLog')->add($data);
						$pBug->setField('state',intval($_POST['state']),'id='.$intId);
						if(intval($_POST['state'])=='3'){
													
            				//通知
            				$body_data['id'] = $intId;
            				$body_data['title'] = $info['title'];
            				$this->api->notify_setAppId($this->appId);
            				$this->api->notify_send($info['uid'],'bug_reply',$title_data,$body_data,$url,'notification');
						}
						if(intval($_POST['state'])=='4'){
            				//通知
            				$body_data['id'] = $intId;
            				$body_data['title'] = $info['title'];
            				$this->api->notify_setAppId($this->appId);
            				$this->api->notify_send($info['uid'],'bug_close',$title_data,$body_data,$url,'notification');
						}						
						$this->redirect('Bug/view/id/'.$intId);						
					}else{
						$this->error('提交失败');
					}
				break;
			}
		}else{
			$this->error('提交错误参数');
		}
	}
	
	//评论发送动态
	public function commentSuccess(){
		$result = json_decode(stripslashes($_POST['data']));  //json被反解析成了stdClass类型
		//计数更新

		 //发送两条消息
		$data['toUid'] = $result->toUid;
		$need  = D('Bug')->where('id='.$result->appid)->field('uid,uname,title')->find();
		$data['uids'] =$need['uid'];
		$data['url'] = sprintf('%s/apps/bug/index.php?s=/Bug/view/id/%s/',
									'{WR}',$result->appid);
		$data['title_body']['comment'] = $result->comment;
		$data['title_data']['title'] = sprintf("<a href='%s'>%s</a>",$data['url'],$need['title']);
		$data['title_data']['type']  = " Bug反馈 ";
		$this->api->comment_notify('bug',$data,$this->appId);
		echo $count;
	}
	
    private function __wipeVerticalArray($array) {
            $result = array();
            foreach($array as $key=>$value) {
                    $temp = explode('|', $value);
                    $result[$key]['id'] = $temp[0];
                    $result[$key]['name'] = $temp[1];
            }
            return $result;
    }	
	
}


?>